package com.szxy.hotel.controller;

import com.szxy.hotel.entity.Client;
import com.szxy.hotel.entity.Viprecode;
import com.szxy.hotel.service.ClientService;
import com.szxy.hotel.service.VipService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Controller
public class ClientController {
    @Resource(name = "clientService")
    ClientService clientService;
    @Resource(name = "vipService")
    VipService vipService;
    /**
     * 用户充值
     */
    @RequestMapping("/vip.do")
    @ResponseBody
    public Map<String,Object> vip(String userName,String Identity,Double Money,String Phone){
        Map<String, Object> map = new HashMap<String, Object>();
        //查询是否有这个用户
        Client client = clientService.findByclientname(userName);
        //判断充值数额是不是小于零
        if(Money<0){
            map.put("status",1);
            map.put("message","充值失败,数值不能为负数");
        }else {
            if(client == null){
                //为空时直接插入数据
                int result = clientService.insertClient(userName,Identity,Money,Phone);
                //下面判断第一次充值相对应的会员
                if(result == 1){
                    Client client1 = clientService.findByclientname(userName);
                    String vipType = "";
                    String vipDiscount = "";
                    Double vipDisDouble = 0.0;
                    if(Money>=0&&Money<1000){
                        vipType = "普通用户";
                        vipDiscount = "不打折";
                        vipDisDouble = 1.0;
                    }else if(Money>=1000&&Money<2000){
                        vipType = "普通会员";
                        vipDiscount = "9折";
                        vipDisDouble = 0.9;
                    }else if(Money>=2000&&Money<5000){
                        vipType = "高级会员";
                        vipDiscount = "8折";
                        vipDisDouble = 0.8;
                    }else if(Money>=5000&&Money<10000){
                        vipType = "白金会员";
                        vipDiscount = "7折";
                        vipDisDouble = 0.7;
                    }else if(Money>=10000){
                        vipType = "至尊会员";
                        vipDiscount = "6折";
                        vipDisDouble = 0.6;
                    }
                    vipService.insertVip(userName,Money,vipType,vipDiscount,vipDisDouble);
                    int id = client1.getClientId();
                    vipService.insertViprecode(id,vipType,Money);
                    map.put("status",0);
                    map.put("message","充值成功");
                }else {
                    map.put("status",1);
                    map.put("message","充值失败");
                }
            }else {
                //不为空时直接更新数据
                Double money = client.getClientMoney();
                money += Money;
                //判断条件  手机号和身份证号
                if(Identity.equals(client.getClientIdentity())&&Phone.equals(client.getClientPhone())){
                    int result = clientService.updateClient(userName,money);
                    if(result == 1){
                        int id = client.getClientId();
                        Viprecode viprecode = vipService.selectViprecode(id);
                        Double VRmoney = viprecode.getViprecodeMoney();
                        if(Money>VRmoney){
                            String vipType = "";
                            String vipDiscount = "";
                            Double vipDisDouble = 0.0;
                            if(Money>=0&&Money<1000){
                                vipType = "普通用户";
                                vipDiscount = "不打折";
                                vipDisDouble = 1.0;
                            }else if(Money>=1000&&Money<2000){
                                vipType = "普通会员";
                                vipDiscount = "9折";
                                vipDisDouble = 0.9;
                            }else if(Money>=2000&&Money<5000){
                                vipType = "高级会员";
                                vipDiscount = "8折";
                                vipDisDouble = 0.8;
                            }else if(Money>=5000&&Money<10000){
                                vipType = "白金会员";
                                vipDiscount = "7折";
                                vipDisDouble = 0.7;
                            }else if(Money>=10000){
                                vipType = "至尊会员";
                                vipDiscount = "6折";
                                vipDisDouble = 0.6;
                            }
                            vipService.updateViprecode(id,vipType,Money);
                            vipService.updateVip(userName,money,vipType,vipDiscount,vipDisDouble);
                        }else {
                            vipService.updateVipMoney(userName,money);
                        }
                        map.put("status",0);
                        map.put("message","充值成功");
                    }else {
                        map.put("status",1);
                        map.put("message","充值失败");
                    }
                }else {
                    map.put("status",1);
                    map.put("message","身份证或者手机号校验错误");
                }
            }
        }
        return map;
    }
}
